package com.example.demo.service.impl;

import com.example.demo.dto.WorkflowDTO;
import com.example.demo.entity.Workflow;
import com.example.demo.mapper.WorkflowMapper;
import com.example.demo.service.WorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorkflowServiceImpl implements WorkflowService {

    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public List<WorkflowDTO> getWorkflows(int page, int size, String flowName) {
        int offset = page * size;
        List<Workflow> workflows = workflowMapper.selectPage(offset, size, flowName);
        return workflows.stream()
                .map(WorkflowDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public long countWorkflows(String flowName) {
        return workflowMapper.count(flowName);
    }

    @Override
    public WorkflowDTO getWorkflowById(Long id) {
        Workflow workflow = workflowMapper.selectById(id);
        if (workflow == null) {
            return null;
        }
        return WorkflowDTO.fromEntity(workflow);
    }

    @Override
    @Transactional
    public WorkflowDTO createWorkflow(WorkflowDTO workflowDTO) {
        //log.info("创建流程，流程名称: {}", workflowDTO.getFlowName());

        Workflow workflow = workflowDTO.toEntity();
        workflow.setCreatedAt(LocalDateTime.now());
        workflow.setUpdatedAt(LocalDateTime.now());

        int result = workflowMapper.insert(workflow);
        if (result == 0) {
            //log.error("流程创建失败，流程名称: {}", workflowDTO.getFlowName());
            throw new RuntimeException("流程创建失败");
        }

        return WorkflowDTO.fromEntity(workflow);
    }

    @Override
    @Transactional
    public WorkflowDTO updateWorkflow(Long id, WorkflowDTO workflowDTO) {
        //log.info("更新流程，流程ID: {}", id);

        Workflow existingWorkflow = workflowMapper.selectById(id);
        if (existingWorkflow == null) {
            //log.warn("要更新的流程不存在，流程ID: {}", id);
            throw new RuntimeException("流程不存在");
        }

        Workflow workflow = workflowDTO.toEntity();
        workflow.setId(id);
        workflow.setCreatedAt(existingWorkflow.getCreatedAt());
        workflow.setUpdatedAt(LocalDateTime.now());

        int result = workflowMapper.updateById(workflow);
        if (result == 0) {
            //log.error("流程更新失败，流程ID: {}", id);
            throw new RuntimeException("流程更新失败");
        }

        return WorkflowDTO.fromEntity(workflow);
    }

    @Override
    @Transactional
    public void deleteWorkflow(Long id) {
        //log.info("删除流程，流程ID: {}", id);

        Workflow existingWorkflow = workflowMapper.selectById(id);
        if (existingWorkflow == null) {
            //log.warn("要删除的流程不存在，流程ID: {}", id);
            throw new RuntimeException("流程不存在");
        }

        int result = workflowMapper.deleteById(id);
        if (result == 0) {
            //log.error("流程删除失败，流程ID: {}", id);
            throw new RuntimeException("流程删除失败");
        }
    }

    @Override
    public List<WorkflowDTO> getAllWorkflowsOrdered() {
        List<Workflow> workflows = workflowMapper.selectAllOrdered();
        return workflows.stream()
                .map(WorkflowDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public WorkflowDTO getWorkflowByOrder(Integer workflowOrder) {
        Workflow workflow = workflowMapper.selectByWorkflowOrder(workflowOrder);
        if (workflow == null) {
            return null;
        }
        return WorkflowDTO.fromEntity(workflow);
    }
}